import {
  Controller,
  Get,
  Post,
  Headers,
  Body,
  Param,
  Delete,
  Query,
  Put,
} from "@nestjs/common";
import { DictionaryService } from "./dictionary.service";
import { DictionaryDto } from "./dto/dictionary.dto";
import {
  ApiBody,
  ApiOkResponse,
  ApiOperation,
  ApiParam,
  ApiResponse,
  ApiTags,
} from "@nestjs/swagger";
import { ResponseResult } from "src/common/tools/response.result";
import { Roles } from "@/common/decorators/roles.decorator";
import { Role } from "@/common/enums/role.enum";
import { DictionaryEntity } from "./entities/dictionary.entity";
import { DictionaryListResult } from "./result/dictionary.list.result";
import { DictionaryResult } from "./result/dictionary.result";

@ApiTags("字典管理")
@Controller("/system/dictionaries")
export class DictionaryController {
  constructor(private readonly dictionaryService: DictionaryService) {}

  @ApiOperation({ summary: "根据分类编码获取字典列表" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: DictionaryListResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Get()
  async getDictionaryListByTypeId(
    @Headers("tenant-id") tenantId: number,
    @Query("typeCode") typeCode
  ) {
    return await this.dictionaryService.getDictionaryListByTypeId(
      tenantId,
      typeCode
    );
  }

  @ApiOperation({ summary: "详情" })
  @ApiParam({ name: "id", type: String, description: "编号", required: true })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: DictionaryResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Get(":id")
  getDictionaryById(
    @Headers("tenant-id") tenantId: number,
    @Param("id") id: string
  ) {
    return this.dictionaryService.getDictionaryById(tenantId, +id);
  }

  @ApiOperation({ summary: "根据字典名称获取字典详情" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: DictionaryResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Get("getDictionaryByDictionaryName/:dictionaryName")
  getDictionaryByDictionaryName(
    @Headers("tenant-id") tenantId: number,
    @Param("dictionaryName") dictionaryName: string
  ) {
    return this.dictionaryService.getDictionaryByDictionaryName(
      tenantId,
      dictionaryName
    );
  }

  @ApiOperation({ summary: "创建" })
  @ApiBody({ type: DictionaryDto, description: "创建字典参数" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Post()
  createDictionary(
    @Headers("tenant-id") tenantId: number,
    @Body() dictionaryDto: DictionaryDto
  ) {
    return this.dictionaryService.createDictionary(tenantId, dictionaryDto);
  }

  @ApiOperation({ summary: "修改" })
  @ApiParam({ name: "id", type: String, description: "编号", required: true })
  @ApiBody({ type: DictionaryDto, description: "修改字典参数" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Put(":id")
  updateDictionaryById(
    @Headers("tenant-id") tenantId: number,
    @Param("id") id: string,
    @Body() dictionaryDto: DictionaryDto
  ) {
    return this.dictionaryService.updateDictionaryById(
      tenantId,
      +id,
      dictionaryDto
    );
  }

  @ApiOperation({ summary: "删除" })
  @ApiOkResponse({
    status: 200,
    description: "操作成功",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 201,
    description: "参数错误",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 401,
    description: "无权限",
    type: ResponseResult,
  })
  @ApiResponse({
    status: 500,
    description: "系统异常",
    type: ResponseResult,
  })
  @Delete(":id")
  removeDictionaryById(
    @Headers("tenant-id") tenantId: number,
    @Param("id") id: string
  ) {
    return this.dictionaryService.removeDictionaryById(tenantId, +id);
  }
}
